# This code replicates Figures 6, A13-A14

library(ggplot2)
library(patchwork)
library(dplyr)

# Figure 6 ----------------------------------------------------------------

# this figure does not use data

fn <- function(prob.attend, Delta){
  mean(sapply(1:4, FUN = function(x){(prob.attend + (x - 2.5)* Delta)*(4-x)/2}))
}

fn2 <- function(prob.attend, Delta){
  sum(sapply(1:4, FUN = function(x){(prob.attend + (x - 2.5)* Delta)/(4 *prob.attend) *(4-x)/2}))
}

ggsave(data.frame(est = 
                    c(sapply(seq(0, 1/3, length.out = 20), FUN = fn, prob.attend = .5),
                      sapply(seq(0, .2, length.out = 20), FUN = fn, prob.attend = .3),
                      sapply(seq(0, 2/30, length.out = 20), FUN = fn, prob.attend = .1),
                      sapply(seq(0, 1/3, length.out = 20), FUN = fn2, prob.attend = .5),
                      sapply(seq(0, .2, length.out = 20), FUN = fn2, prob.attend = .3),
                      sapply(seq(0, 2/30, length.out = 20), FUN = fn2, prob.attend = .1)),
                  prob.attend = rep(rep(c(.5, .3, .1), each = 20)),
                  theta = rep(seq(0, 2, length.out = 20), 6), 
                  estimand = rep(c("ITT", "ATT"), each = 60)) %>%
         mutate(estimand = factor(estimand, levels = unique(estimand))) %>%
         ggplot(aes(x = theta, y = est)) + 
         geom_point(aes( col = as.factor(prob.attend)), alpha = .75) + 
         geom_line(aes(col = as.factor(prob.attend)), alpha = .75) + 
         facet_grid(~estimand) + 
         scale_color_manual("Compliance rate", values = c("#41b6c4", "#2c7fb8", "#253494")) + 
         scale_fill_manual("Compliance rate", values = c("#41b6c4", "#2c7fb8", "#253494")) + 
         theme_minimal() + 
         scale_x_continuous("Degree of positive selection") + 
         ylab("Treatment effect") + 
         theme(legend.position = "bottom"), file = "results/Figure_6.pdf", width = 8, height = 3)



# Figure A13 --------------------------------------------------------------
 load("data/mtgs_gender.Rdata")

ggsave(ggplot(attend) +
  geom_histogram(aes(x = wm_share),
                 color = "black", fill = "gray50", binwidth = .1) +
  geom_vline(aes(xintercept = 0),
             color = "red", linetype = "dashed") +
  theme_bw(base_size = 14) +
  theme() +
  xlab("Meeting participation by gender (women-men)/total"),
  file = "results/Figure_A13.pdf", width = 6, height = 4)
  
# Figure A14 --------------------------------------------------------------

load("data/sentiment.Rdata")

fig_a14a <- ggplot(sentiment, aes(x = mean_bl_trust, y = n_mmetings)) + 
  geom_jitter(height = .1) + 
  geom_smooth(se = F) +
  scale_x_continuous("Beat average trust in police at baseline\n(4-point likert scale)", limits = c(1.75, 3.25)) + 
  scale_y_continuous("Number of meetings held (not cancelled)") + 
  geom_smooth(method = "lm", col = "red", lty = 2, se = F) + 
  theme_minimal()

fig_a14b <- ggplot(sentiment, aes(x = mean_bl_trust, y = mean_sentiment)) + 
  geom_point() +
  geom_smooth(se = F) +
  scale_x_continuous("Beat average trust in police at baseline\n(4-point likert scale)", limits = c(1.75, 3.25)) + 
  scale_y_continuous("Meeting sentiment index (z-score) | Meeting held") + 
  geom_smooth(method = "lm", col = "red", lty = 2, se = F) +
  theme_minimal()


ggsave(fig_a14a + fig_a14b, file = "results/Figure_A14.pdf", width = 9, height = 4)

